<template>
  <div class="app-container">
     <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch">
       <el-form-item label="角色">
         <el-input :value="rowData.name" disabled/>
       </el-form-item>
      <el-form-item label="用户名称" prop="like.user_name">
        <el-input v-model="searchForm.like.user_name" placeholder="请输入用户名称" clearable />
      </el-form-item>
      <el-form-item label="手机号码" prop="like.phone">
        <el-input v-model="searchForm.like.phone" placeholder="请输入手机号码" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px reset">
      <el-button type="primary" plain icon="el-icon-plus" @click="selectShow = true" v-hasPerm="['system:role:auth-list']">添加用户</el-button>
      <el-button type="danger" plain icon="el-icon-circle-close" @click="cancelAuthUser" v-hasPerm="['system:role:auth-list']" :disabled="!ids.length" >批量取消授权</el-button>
      <el-button type="warning" plain icon="el-icon-close" @click="close">关闭</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList" />
    </div>

    <el-table v-loading="loading" :data="listData" @selection-change="handleSelection">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" width="80" align="center" />
      <el-table-column label="用户名称" prop="user_name" show-overflow-tooltip />
      <el-table-column label="用户昵称" prop="nick_name" show-overflow-tooltip />
      <el-table-column label="邮箱" prop="email" show-overflow-tooltip />
      <el-table-column label="手机" prop="phone" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthUser(scope.row)"
            v-hasPerm="['system:role:auth-list']"
          >取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="searchForm.pageNum"
      :limit.sync="searchForm.pageSize"
      @pagination="getList"
    />
    <select-user v-model="selectShow" :roleId="searchForm.roleId" @submit="search" v-if="selectShow"/>
  </div>
</template>

<script>
import { authUsersListApi, cancelAuthUserApi, infoRoleApi } from '@/api/system/role'
import SelectUser from "./selectUser"

export default {
  name: "AuthUser",
  dicts: ['sys_normal_disable'],
  components: { SelectUser },
  data() {
    return {
      loading: true,
      ids: [],
      showSearch: true,
      rowData: {},
      total: 0,
      listData: [],
      selectShow: false,
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        like: { user_name: undefined, phone: undefined}
      }
    }
  },
  created() {
    const roleId = this.$route.params?.roleId || ''
    if(!roleId) return this.close(null, '角色参数错误')
    this.searchForm.roleId = roleId
    this.getRowData()
  },
  methods: {
    getList() {
      this.loading = true
      authUsersListApi(this.searchForm).then(res => {
        this.listData = res.list
        this.total = res.total
        this.loading = false
      })
    },
    close(evt,err) {
      if (err) this.$modal.msgError(err)
      const obj = { path: "/system/role" }
      this.$tab.closeOpenPage(obj)
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },
    reset() {
      this.resetForm('searchFormRef')
      this.search()
    },
    async getRowData() {
      try {
        const { info } = await infoRoleApi(this.searchForm.roleId)
        this.rowData = info
        this.getList()
      } catch (error) {
        this.close(null,error)
      }
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.id)
    },
    cancelAuthUser(row) {
      const roleId = this.searchForm.roleId
      const adminIds = row?.id || this.ids.join(',')
      this.$modal.confirm('确认要取消用户编号为"' + adminIds + '"授权角色吗？').then(function() {
        return cancelAuthUserApi(roleId, adminIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("取消授权成功")
      }).catch(() => {})
    },
  }
}
</script>
